﻿using System;
using System.Windows.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VideoReflector.Models;
using VideoReflector.Controllers;

namespace VideoReflector.Converters
{
    /// <summary>
    /// Converter taking collection of ClipTag objects as the input value plus a ClipTagGroup ID as parameter
    /// and returns a collection of ClipTag objects with a matching GroupID
    /// </summary>
    [ValueConversion(typeof(IList<ClipTag>), typeof(IList<ClipTag>))]
    public class ClipFilterConverter : ConverterWithMarkupExtension, IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            try
            {
                var tagCollection = (IList<ClipTag>)value;
                var groupID = (int)parameter;
                var tagQuery = from tag in tagCollection where tag.GroupID == groupID select tag;
                return tagQuery.ToList();
            }
            catch (InvalidCastException e)
            {
                throw new ApplicationException("ClipFilterConverter requires a value of type ICollection<ClipTag> and parameter of type int", e);
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
